package org.iori.luodan.dao;

import java.util.HashMap;
import java.util.List;


import com.iori.common.db.DataAccessorFactory;
import com.iori.common.db.dbctrl.DataAccessor;
import com.iori.common.log.FounderLog;
import com.iori.common.log.LogFactory;

/**
 * [クラス名]<br>
 * GoodsDao.java<br>
 * [機能概要]<br>
 * 検索<br>
 * <br>
 * [変更履歴]<br>
 * 2012/10/04 ver1.00 新規作成<br>
 * 
 * @author luodan CORPORATION lun_xing
 * @version 1.00
 */
public class GoodsDao {

	//	Log出力クラスを定義
	private static FounderLog log = LogFactory.getFounderLog(GoodsDao.class);

	/**	商品情報の取得
	 * 
	 */
	public HashMap getGoods(int goods_id) throws Exception {
		HashMap map = null;
		try {
			List resultList = null;
			DataAccessor da = DataAccessorFactory
					.getDataAccessor("GoodsDao.getGoods");
			da.setParameter(":GOODS_ID", goods_id);
			resultList = da.executeQueryAsList();
			System.out.println("#################resultList count=" + resultList.size());
			if (resultList != null && resultList.size() > 0) {
				map = (HashMap)resultList.get(0);
			}
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			log.error(e);
			throw e;
		}
		return map;
	}
	
	/**	商品のリストの取得
	 * 
	 */
	public List getGoodsList(int cat_id, int start_row, int row_count) throws Exception {
		List resultList = null;
		try {
			DataAccessor da = DataAccessorFactory
					.getDataAccessor("GoodsDao.getGoodsList");
			da.setParameter(":CAT_ID", cat_id);
			da.setParameter("#start_row", start_row);
			da.setParameter("#row_count", row_count);
			resultList = da.executeQueryAsList();
			System.out.println("#################resultList count=" + resultList.size());
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			log.error(e);
			throw e;
		}
		return resultList;
	}
	
	/**	該当商品販売の数 
	 * 
	 */
	public int getSalesCount(int goods_id) throws Exception {
		int salesCount = 0;
		List resultList = null;
		try {
			DataAccessor da = DataAccessorFactory
					.getDataAccessor("GoodsDao.getSalesCount");
			da.setParameter(":GOODS_ID", goods_id);
			resultList = da.executeQueryAsList();
			if (resultList != null) {
			    log.debug("resultListのサイズ：" + resultList.size());
			}
			if (resultList != null && resultList.size() > 0) {
				HashMap map = (HashMap)resultList.get(0);
				salesCount = Integer.valueOf(map.get("NUM").toString().trim());
			}
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			log.error(e);
			throw e;
		}
		return salesCount;
	}
	
	/**	該当商品販売の数 
	 * 
	 */
	public int getRankNum(int salesCount) throws Exception {
		int rankNum = 0;
		List resultList = null;
		try {
			DataAccessor da = DataAccessorFactory
					.getDataAccessor("GoodsDao.getRankNum");
			da.setParameter(":SALESCOUNT", salesCount);
			resultList = da.executeQueryAsList();
			if (resultList != null) {
			    log.debug("resultListのサイズ：" + resultList.size());
			}
			if (resultList != null && resultList.size() > 0) {
				HashMap map = (HashMap)resultList.get(0);
				rankNum = Integer.valueOf(map.get("NUM").toString().trim()) + 1;
			}
			if (rankNum > 10) {
				rankNum = 0;
			}
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			log.error(e);
			throw e;
		}
		return rankNum;
	}
	
	/**	該当commen rank 
	 * 
	 */
	public int getCommentRank(int goods_id) throws Exception {
		int commentRank = 0;
		List resultList = null;
		try {
			DataAccessor da = DataAccessorFactory
					.getDataAccessor("GoodsDao.getCommentRank");
			da.setParameter(":GOODS_ID", goods_id);
			resultList = da.executeQueryAsList();
			if (resultList != null) {
			    log.debug("resultListのサイズ：" + resultList.size());
			}
			if (resultList != null && resultList.size() > 0) {
				HashMap map = (HashMap)resultList.get(0);
				commentRank = Integer.valueOf(map.get("COMMENT_RANK").toString().trim()) + 1;
			}
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			log.error(e);
			throw e;
		}
		return commentRank;
	}
}
